home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / svc.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  12KB  |  512 lines

  1. /*
  2.  * $Id: svc.c,v 1.2 1993/11/10 02:45:05 jraja Exp $
  3.  *
  4.  * $Log: svc.c,v $
  5.  * Revision 1.2  1993/11/10  02:45:05  jraja
  6.  * Fixed includes, ANSI prototypes.
  7.  * Added ffs() function.
  8.  * Added clearing of allocated pointer array on AMIGA (not needed on UNIX?).
  9.  *
  10.  */
  11. /* @(#)svc.c    2.4 88/08/11 4.0 RPCSRC; from 1.44 88/02/08 SMI */
  12. /*
  13.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  14.  * unrestricted use provided that this legend is included on all tape
  15.  * media and as a part of the software program in whole or part.  Users
  16.  * may copy or modify Sun RPC without charge, but are not authorized
  17.  * to license or distribute it to anyone else except as part of a product or
  18.  * program developed by the user.
  19.  * 
  20.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  21.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  23.  * 
  24.  * Sun RPC is provided with no support and without any obligation on the
  25.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  26.  * modification or enhancement.
  27.  * 
  28.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  29.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  30.  * OR ANY PART THEREOF.
  31.  * 
  32.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  33.  * or profits or other special, indirect and consequential damages, even if
  34.  * Sun has been advised of the possibility of such damages.
  35.  * 
  36.  * Sun Microsystems, Inc.
  37.  * 2550 Garcia Avenue
  38.  * Mountain View, California  94043
  39.  */
  40. #if !defined(lint) && defined(SCCSIDS)
  41. static char sccsid[] = "@(#)svc.c 1.41 87/10/13 Copyr 1984 Sun Micro";
  42. #endif
  43.  
  44. /*
  45.  * svc.c, Server-side remote procedure call interface.
  46.  *
  47.  * There are two sets of procedures here.  The xprt routines are
  48.  * for handling transport handles.  The svc routines handle the
  49.  * list of service routines.
  50.  *
  51.  * Copyright (C) 1984, Sun Microsystems, Inc.
  52.  */
  53.  
  54. #include <sys/param.h>
  55. #include <errno.h>
  56. #include <rpc/rpc.h>
  57. #include <rpc/pmap_clnt.h>
  58. #include <sys/syslog.h>
  59.  
  60. #ifdef FD_SETSIZE
  61. static SVCXPRT **xports;
  62. #else
  63. #define NOFILE 32
  64.  
  65. static SVCXPRT *xports[NOFILE];
  66. #endif /* def FD_SETSIZE */
  67.  
  68. #ifdef FD_SETSIZE
  69. /*
  70.  * ffs() copied directly from bsdss/server/kern/subr_xxx.c
  71.  */
  72. static __inline int ffs(register long mask)
  73. {
  74.   register int bit;
  75.  
  76.   if (!mask)
  77.     return(0);
  78.   for (bit = 1;; ++bit) {
  79.     if (mask&0x01)
  80.       return(bit);
  81.     mask >>= 1;
  82.   }
  83.   /* NOT REACHED */
  84. }
  85. #endif /* def FD_SETSIZE */
  86.  
  87. #define NULL_SVC ((struct svc_callout *)0)
  88. #define    RQCRED_SIZE    400        /* this size is excessive */
  89.  
  90. static struct svc_callout *
  91. svc_find(u_long prog, u_long vers, struct svc_callout **prev);
  92.  
  93. /*
  94.  * The services list
  95.  * Each entry represents a set of procedures (an rpc program).
  96.  * The dispatch routine takes request structs and runs the
  97.  * appropriate procedure.
  98.  */
  99. static struct svc_callout {
  100.     struct svc_callout *sc_next;
  101.     u_long            sc_prog;
  102.     u_long            sc_vers;
  103.     void            (* sc_dispatch)(struct svc_req *, SVCXPRT *);
  104. } *svc_head;
  105.  
  106. static struct svc_callout *svc_find();
  107.  
  108. /* ***************  SVCXPRT related stuff **************** */
  109.  
  110. /*
  111.  * Activate a transport handle.
  112.  */
  113. void
  114. xprt_register(xprt)
  115.     SVCXPRT *xprt;
  116. {
  117.     register int sock = xprt->xp_sock;
  118.  
  119. #ifdef FD_SETSIZE
  120.     if (xports == NULL) {
  121.         xports = (SVCXPRT **)
  122.             mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
  123. #ifdef AMIGA /* allocated area MUST be cleared */
  124.         bzero(xports, FD_SETSIZE * sizeof(SVCXPRT *));
  125. #endif
  126.     }
  127.     if (sock < _rpc_dtablesize()) {
  128.         xports[sock] = xprt;
  129.         FD_SET(sock, &svc_fdset);
  130.     }
  131. #else
  132.     if (sock < NOFILE) {
  133.         xports[sock] = xprt;
  134.         svc_fds |= (1 << sock);
  135.     }
  136. #endif /* def FD_SETSIZE */
  137.  
  138. }
  139.  
  140. /*
  141.  * De-activate a transport handle. 
  142.  */
  143. void
  144. xprt_unregister(xprt) 
  145.     SVCXPRT *xprt;
  146.     register int sock = xprt->xp_sock;
  147.  
  148. #ifdef FD_SETSIZE
  149.     if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) {
  150.         xports[sock] = (SVCXPRT *)0;
  151.         FD_CLR(sock, &svc_fdset);
  152.     }
  153. #else
  154.     if ((sock < NOFILE) && (xports[sock] == xprt)) {
  155.         xports[sock] = (SVCXPRT *)0;
  156.         svc_fds &= ~(1 << sock);
  157.     }
  158. #endif /* def FD_SETSIZE */
  159. }
  160.  
  161.  
  162. /* ********************** CALLOUT list related stuff ************* */
  163.  
  164. /*
  165.  * Add a service program to the callout list.
  166.  * The dispatch routine will be called when a rpc request for this
  167.  * program number comes in.
  168.  */
  169. bool_t
  170. svc_register(xprt, prog, vers, dispatch, protocol)
  171.     SVCXPRT *xprt;
  172.     u_long prog;
  173.     u_long vers;
  174.     void (* dispatch)(struct svc_req *, SVCXPRT *);
  175.     int protocol;
  176. {
  177.     struct svc_callout *prev;
  178.     register struct svc_callout *s;
  179.  
  180.     if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
  181.         if (s->sc_dispatch == dispatch)
  182.             goto pmap_it;  /* he is registering another xptr */
  183.         return (FALSE);
  184.     }
  185.     s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
  186.     if (s == (struct svc_callout *)0) {
  187.         return (FALSE);
  188.     }
  189.     s->sc_prog = prog;
  190.     s->sc_vers = vers;
  191.     s->sc_dispatch = dispatch;
  192.     s->sc_next = svc_head;
  193.     svc_head = s;
  194. pmap_it:
  195.     /* now register the information with the local binder service */
  196.     if (protocol) {
  197.         return (pmap_set(prog, vers, protocol, xprt->xp_port));
  198.     }
  199.     return (TRUE);
  200. }
  201.  
  202. /*
  203.  * Remove a service program from the callout list.
  204.  */
  205. void
  206. svc_unregister(prog, vers)
  207.     u_long prog;
  208.     u_long vers;
  209. {
  210.     struct svc_callout *prev;
  211.     register struct svc_callout *s;
  212.  
  213.     if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
  214.         return;
  215.     if (prev == NULL_SVC) {
  216.         svc_head = s->sc_next;
  217.     } else {
  218.         prev->sc_next = s->sc_next;
  219.     }
  220.     s->sc_next = NULL_SVC;
  221.     mem_free((char *) s, (u_int) sizeof(struct svc_callout));
  222.     /* now unregister the information with the local binder service */
  223.     (void)pmap_unset(prog, vers);
  224. }
  225.  
  226. /*
  227.  * Search the callout list for a program number, return the callout
  228.  * struct.
  229.  */
  230. static struct svc_callout *
  231. svc_find(u_long prog, u_long vers, struct svc_callout **prev)
  232. {
  233.     register struct svc_callout *s, *p;
  234.  
  235.     p = NULL_SVC;
  236.     for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  237.         if ((s->sc_prog == prog) && (s->sc_vers == vers))
  238.             goto done;
  239.         p = s;
  240.     }
  241. done:
  242.     *prev = p;
  243.     return (s);
  244. }
  245.  
  246. /* ******************* REPLY GENERATION ROUTINES  ************ */
  247.  
  248. /*
  249.  * Send a reply to an rpc request
  250.  */
  251. bool_t
  252. svc_sendreply(xprt, xdr_results, xdr_location)
  253.     register SVCXPRT *xprt;
  254.     xdrproc_t xdr_results;
  255.     caddr_t xdr_location;
  256. {
  257.     struct rpc_msg rply; 
  258.  
  259.     rply.rm_direction = REPLY;  
  260.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  261.     rply.acpted_rply.ar_verf = xprt->xp_verf; 
  262.     rply.acpted_rply.ar_stat = SUCCESS;
  263.     rply.acpted_rply.ar_results.where = xdr_location;
  264.     rply.acpted_rply.ar_results.proc = xdr_results;
  265.     return (SVC_REPLY(xprt, &rply)); 
  266. }
  267.  
  268. /*
  269.  * No procedure error reply
  270.  */
  271. void
  272. svcerr_noproc(xprt)
  273.     register SVCXPRT *xprt;
  274. {
  275.     struct rpc_msg rply;
  276.  
  277.     rply.rm_direction = REPLY;
  278.     rply.rm_reply.rp_stat = MSG_ACCEPTED;
  279.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  280.     rply.acpted_rply.ar_stat = PROC_UNAVAIL;
  281.     SVC_REPLY(xprt, &rply);
  282. }
  283.  
  284. /*
  285.  * Can't decode args error reply
  286.  */
  287. void
  288. svcerr_decode(xprt)
  289.     register SVCXPRT *xprt;
  290. {
  291.     struct rpc_msg rply; 
  292.  
  293.     rply.rm_direction = REPLY; 
  294.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  295.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  296.     rply.acpted_rply.ar_stat = GARBAGE_ARGS;
  297.     SVC_REPLY(xprt, &rply); 
  298. }
  299.  
  300. /*
  301.  * Some system error
  302.  */
  303. void
  304. svcerr_systemerr(xprt)
  305.     register SVCXPRT *xprt;
  306. {
  307.     struct rpc_msg rply; 
  308.  
  309.     rply.rm_direction = REPLY; 
  310.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  311.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  312.     rply.acpted_rply.ar_stat = SYSTEM_ERR;
  313.     SVC_REPLY(xprt, &rply); 
  314. }
  315.  
  316. /*
  317.  * Authentication error reply
  318.  */
  319. void
  320. svcerr_auth(xprt, why)
  321.     SVCXPRT *xprt;
  322.     enum auth_stat why;
  323. {
  324.     struct rpc_msg rply;
  325.  
  326.     rply.rm_direction = REPLY;
  327.     rply.rm_reply.rp_stat = MSG_DENIED;
  328.     rply.rjcted_rply.rj_stat = AUTH_ERROR;
  329.     rply.rjcted_rply.rj_why = why;
  330.     SVC_REPLY(xprt, &rply);
  331. }
  332.  
  333. /*
  334.  * Auth too weak error reply
  335.  */
  336. void
  337. svcerr_weakauth(xprt)
  338.     SVCXPRT *xprt;
  339. {
  340.  
  341.     svcerr_auth(xprt, AUTH_TOOWEAK);
  342. }
  343.  
  344. /*
  345.  * Program unavailable error reply
  346.  */
  347. void 
  348. svcerr_noprog(xprt)
  349.     register SVCXPRT *xprt;
  350. {
  351.     struct rpc_msg rply;  
  352.  
  353.     rply.rm_direction = REPLY;   
  354.     rply.rm_reply.rp_stat = MSG_ACCEPTED;  
  355.     rply.acpted_rply.ar_verf = xprt->xp_verf;  
  356.     rply.acpted_rply.ar_stat = PROG_UNAVAIL;
  357.     SVC_REPLY(xprt, &rply);
  358. }
  359.  
  360. /*
  361.  * Program version mismatch error reply
  362.  */
  363. void  
  364. svcerr_progvers(xprt, low_vers, high_vers)
  365.     register SVCXPRT *xprt; 
  366.     u_long low_vers;
  367.     u_long high_vers;
  368. {
  369.     struct rpc_msg rply;
  370.  
  371.     rply.rm_direction = REPLY;
  372.     rply.rm_reply.rp_stat = MSG_ACCEPTED;
  373.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  374.     rply.acpted_rply.ar_stat = PROG_MISMATCH;
  375.     rply.acpted_rply.ar_vers.low = low_vers;
  376.     rply.acpted_rply.ar_vers.high = high_vers;
  377.     SVC_REPLY(xprt, &rply);
  378. }
  379.  
  380. /* ******************* SERVER INPUT STUFF ******************* */
  381.  
  382. /*
  383.  * Get server side input from some transport.
  384.  *
  385.  * Statement of authentication parameters management:
  386.  * This function owns and manages all authentication parameters, specifically
  387.  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
  388.  * the "cooked" credentials (rqst->rq_clntcred).
  389.  * However, this function does not know the structure of the cooked
  390.  * credentials, so it make the following assumptions: 
  391.  *   a) the structure is contiguous (no pointers), and
  392.  *   b) the cred structure size does not exceed RQCRED_SIZE bytes. 
  393.  * In all events, all three parameters are freed upon exit from this routine.
  394.  * The storage is trivially management on the call stack in user land, but
  395.  * is mallocated in kernel land.
  396.  */
  397.  
  398. void
  399. svc_getreq(rdfds)
  400.     int rdfds;
  401. {
  402. #ifdef FD_SETSIZE
  403.     fd_set readfds;
  404.  
  405.     FD_ZERO(&readfds);
  406.     readfds.fds_bits[0] = rdfds;
  407.     svc_getreqset(&readfds);
  408. #else
  409.     int readfds = rdfds & svc_fds;
  410.  
  411.     svc_getreqset(&readfds);
  412. #endif /* def FD_SETSIZE */
  413. }
  414.  
  415. void
  416. svc_getreqset(readfds)
  417. #ifdef FD_SETSIZE
  418.     fd_set *readfds;
  419. {
  420. #else
  421.     int *readfds;
  422. {
  423.     int readfds_local = *readfds;
  424. #endif /* def FD_SETSIZE */
  425.     enum xprt_stat stat;
  426.     struct rpc_msg msg;
  427.     int prog_found;
  428.     u_long low_vers;
  429.     u_long high_vers;
  430.     struct svc_req r;
  431.     register SVCXPRT *xprt;
  432.     register u_long mask;
  433.     register int bit;
  434.     register u_long *maskp;
  435.     register int setsize;
  436.     register int sock;
  437.     char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
  438.     msg.rm_call.cb_cred.oa_base = cred_area;
  439.     msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
  440.     r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
  441.  
  442.  
  443. #ifdef FD_SETSIZE
  444.     setsize = _rpc_dtablesize();    
  445.     maskp = (u_long *)readfds->fds_bits;
  446.     for (sock = 0; sock < setsize; sock += NFDBITS) {
  447.         for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
  448.         /* sock has input waiting */
  449.         xprt = xports[sock + bit - 1];
  450. #else
  451.     for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
  452.         if ((readfds_local & 1) != 0) {
  453.         /* sock has input waiting */
  454.         xprt = xports[sock];
  455. #endif /* def FD_SETSIZE */
  456.         /* now receive msgs from xprtprt (support batch calls) */
  457.         do {
  458.             if (SVC_RECV(xprt, &msg)) {
  459.  
  460.                 /* now find the exported program and call it */
  461.                 register struct svc_callout *s;
  462.                 enum auth_stat why;
  463.  
  464.                 r.rq_xprt = xprt;
  465.                 r.rq_prog = msg.rm_call.cb_prog;
  466.                 r.rq_vers = msg.rm_call.cb_vers;
  467.                 r.rq_proc = msg.rm_call.cb_proc;
  468.                 r.rq_cred = msg.rm_call.cb_cred;
  469.                 /* first authenticate the message */
  470.                 if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
  471.                     svcerr_auth(xprt, why);
  472.                     goto call_done;
  473.                 }
  474.                 /* now match message with a registered service*/
  475.                 prog_found = FALSE;
  476.                 low_vers = 0 - 1;
  477.                 high_vers = 0;
  478.                 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  479.                     if (s->sc_prog == r.rq_prog) {
  480.                         if (s->sc_vers == r.rq_vers) {
  481.                             (*s->sc_dispatch)(&r, xprt);
  482.                             goto call_done;
  483.                         }  /* found correct version */
  484.                         prog_found = TRUE;
  485.                         if (s->sc_vers < low_vers)
  486.                             low_vers = s->sc_vers;
  487.                         if (s->sc_vers > high_vers)
  488.                             high_vers = s->sc_vers;
  489.                     }   /* found correct program */
  490.                 }
  491.                 /*
  492.                  * if we got here, the program or version
  493.                  * is not served ...
  494.                  */
  495.                 if (prog_found)
  496.                     svcerr_progvers(xprt,
  497.                     low_vers, high_vers);
  498.                 else
  499.                      svcerr_noprog(xprt);
  500.                 /* Fall through to ... */
  501.             }
  502.         call_done:
  503.             if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
  504.                 SVC_DESTROY(xprt);
  505.                 break;
  506.             }
  507.         } while (stat == XPRT_MOREREQS);
  508.         }
  509.     }
  510. }
  511.